package soms_handler

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"strconv"
	"time"
)

type ProductCustomFieldHandler interface {
	ListProductCustomField() gin.HandlerFunc
	GetProductCustomField() gin.HandlerFunc
	AddProductCustomField() gin.HandlerFunc
	EditProductCustomField() gin.HandlerFunc
	DeleteProductCustomField() gin.HandlerFunc
	BatchDeleteProductCustomField() gin.HandlerFunc
}

type productCustomFieldHandler struct {
	somsClient soms.Client
}

func (h *productCustomFieldHandler) ListProductCustomField() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.ListProductCustomFieldDataRequest
		if err := ctx.ShouldBindQuery(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.ListProductCustomField(util.GetRPCContext(ctx), &api.ListProductCustomFieldRequest{
			PageNum:  req.PageNum,
			PageSize: req.PageSize,
			Data: &api.ProductCustomFieldData{
				Name: req.Name,
	Explain: req.Explain,
	Type: req.Type,
	RequiredFlag: req.RequiredFlag,
	PrintFlag: req.PrintFlag,
	Sort: req.Sort,
	OptionValue: req.OptionValue,
	
			},
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if res == nil || res.Data == nil {
			handler.HandleSuccessListDataResponse(ctx, nil, 0)
			return
		}
		handler.HandleSuccessListDataResponse(ctx, res.Data, res.Count)
	}
}

func (h *productCustomFieldHandler) GetProductCustomField() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.GetProductCustomField(util.GetRPCContext(ctx), &api.ProductCustomFieldData{
			Id: id,
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessDataResponse(ctx, res)
	}
}

func (h *productCustomFieldHandler) AddProductCustomField() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var data stru.ProductCustomFieldData
		if err := ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		addData := &api.ProductCustomFieldData{
            Id:         data.Id,
            Name: data.Name,
	Explain: data.Explain,
	Type: data.Type,
	RequiredFlag: data.RequiredFlag,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	OptionValue: data.OptionValue,
	
        }
		err := h.somsClient.AddProductCustomField(util.GetRPCContext(ctx), addData)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldHandler) EditProductCustomField() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		var data stru.ProductCustomFieldData
		if err = ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		editData := &api.ProductCustomFieldData{
            Id:         id,
            Name: data.Name,
	Explain: data.Explain,
	Type: data.Type,
	RequiredFlag: data.RequiredFlag,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	OptionValue: data.OptionValue,
	
        }
		if err = h.somsClient.EditProductCustomField(util.GetRPCContext(ctx), editData); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldHandler) DeleteProductCustomField() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if err = h.somsClient.DeleteProductCustomField(util.GetRPCContext(ctx), id); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldHandler) BatchDeleteProductCustomField() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.BatchDeleteProductCustomFieldRequest
		if err := ctx.ShouldBindJSON(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		if req.Ids == nil || len(req.Ids) == 0 {
			handler.HandleErrorResponse(ctx, handler.HandleNewError("参数错误"))
			return
		}

		if err := h.somsClient.BatchDeleteProductCustomField(util.GetRPCContext(ctx), &api.BatchDeleteProductCustomFieldRequest{
			Ids: req.Ids,
		}); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

